import { _decorator, Component, Node, assetManager, Sprite, tween, Vec3, UIOpacity, Label, find, v3 } from 'cc';
import { CommonAudioManager } from './CommonAudioManager';
const { ccclass, property } = _decorator;

@ccclass('CommonEndingManager_1')
export class CommonEndingManager_1 extends Component {

    @property(CommonAudioManager)
    audioRoot: CommonAudioManager = null!;

    cur_city: string = ""
    cur_platForm: string = ""

    onEnable() {
        let city = this.curCity
        this.cur_city = city

        let platForm = this.curPlatform
        this.cur_platForm = platForm

        if (!this.audioRoot) {
            if (find("Canvas/CommonAudioManager")) {
                this.audioRoot = find("Canvas/CommonAudioManager").getComponent(CommonAudioManager);
            } else {
                let audioManger = find("Canvas").getComponentsInChildren(CommonAudioManager)
                for (const audio of audioManger) {
                    if (audio.audioClips.length >= 0) {
                        this.audioRoot = audio
                        break
                    }
                }
            }
        }

        let part2 = this.node.getChildByName("part2");
        let frame = part2.getChildByName("frame");
        if (this.cur_platForm == "applovin") {
            frame.getChildByName("type_0").active = true;
            frame.getChildByName("type_1").active = false;
        } else {
            frame.getChildByName("type_0").active = false;
            frame.getChildByName("type_1").active = true;
          
        }
        //bigo的RU用单卡
        if (this.cur_city && this.cur_platForm && this.cur_city.toLowerCase() == "ru" && this.cur_platForm.toLowerCase() == "bigo") {
            frame.getChildByName("frame").children[0].active = true
        } else {
            frame.getChildByName("frame").children[0].active = false
        }

        this.initView();
        this.changeEndPlanePos()
    }


    //正常的["AT","AU","CA","CH","CZ","FI","GB","ID","IE","IL","NO","NZ","PH","SG","TH","TR","UA","ZA"]
    resizeCirclePosArr = [
        {
            contry: ["AE", "AR", "BR", "BD", "BE", "CL", "CO", "DE", "EC", "EG", "ES", "ET", "FR", "HU", "IN", "IT", "JP", "KR", "KW",
                "MX", "MY", "NG", "NL", "PE", "PK", "PL", "PT", "QA", "RO", "RU", "SA", "SE", "TW", "VN"], pos: [v3(-201.5, 168), v3(46, 168)]
        },
        { contry: "CR", pos: [v3(-201.5, 168), v3(49, 168)] },
        { contry: "PH", pos: [v3(-200, 170), v3(51, 170)] },
    ]
    changeEndPlanePos() {
        let city = this.cur_city
        if (city) {
            let cicle1 = find("part2/frame/chooseNode/choose_0/circle", this.node)
            let cicle2 = find("part2/frame/chooseNode/choose_1/circle", this.node)
            if (cicle1 && cicle2) {
                this.resizeCirclePosArr.forEach(obj => {
                    let contry = obj.contry
                    let pos = obj.pos
                    if (contry) {
                        if (Array.isArray(contry)) {
                            for (let i = 0; i < contry.length; i++) {
                                let element = contry[i];
                                if (city.toLowerCase() == element.toLowerCase()) {
                                    cicle1.setPosition(pos[0])
                                    cicle2.setPosition(pos[1])
                                    break
                                }
                            }
                        } else {
                            if (pos && city.toLowerCase() == contry.toLowerCase()) {
                                cicle1.setPosition(pos[0])
                                cicle2.setPosition(pos[1])
                            }
                        }

                    }
                })
            }
        }

    }

    setMoneyLable(value) {
        let part2 = this.node.getChildByName("part2");
        let frame = part2.getChildByName("frame");
        frame.getChildByName("money").getComponent(Label).string = value + "";
    }

    public initView() {

        let part1 = this.node.getChildByName("part1");
        let part2 = this.node.getChildByName("part2");
        if (this.cur_city && this.cur_platForm && this.cur_city.toLowerCase() == "jp" && this.cur_platForm.toLowerCase() == "mintegral") {
            let showIndex = 0;
            let frame = part2.getChildByName("frame");
            let chooseNode = frame.getChildByName("chooseNode").children;
            chooseNode[0].active = (showIndex == 1 ? true : false);
            chooseNode[1].active = (showIndex == 1 ? false : true);
        } else if (this.cur_city && this.cur_platForm && this.cur_city.toLowerCase() == "ru" && this.cur_platForm.toLowerCase() == "bigo") {
            let showIndex = 1;
            let frame = part2.getChildByName("frame");
            let chooseNode = frame.getChildByName("chooseNode").children;
            chooseNode[0].active = (showIndex == 1 ? true : false);
            chooseNode[1].active = (showIndex == 1 ? false : true);
        }
        else {
            let showIndex = 0;
            this.schedule(() => {
                let frame = part2.getChildByName("frame");
                let chooseNode = frame.getChildByName("chooseNode").children;
                chooseNode[0].active = (showIndex == 1 ? true : false);
                chooseNode[1].active = (showIndex == 1 ? false : true);
                showIndex++;
                showIndex = showIndex % 2;
            }, 2)
        }
        // let showIndex = 0;
        // this.schedule(() => {
        //     let frame = part2.getChildByName("frame");
        //     let chooseNode = frame.getChildByName("chooseNode").children;
        //     chooseNode[0].active = (showIndex == 1 ? true : false);
        //     chooseNode[1].active = (showIndex == 1 ? false : true);
        //     showIndex++;
        //     showIndex = showIndex % 2;
        // }, 2)

        part1.active = true;
        tween(part2)
            .to(0.2, { scale: new Vec3(1, 1, 1) })
            .call(() => {
                this.endPlaneShakeAction();
            })
            .start();
    }

    endPlaneShakeAction() {

        let part2 = this.node.getChildByName("part2");

        let frame = part2.getChildByName("frame");
        let btn = null;
        if (this.cur_platForm == "mintegral" || this.cur_platForm == "unity") {
            btn = frame.getChildByName("type_1").getChildByName("btn_1");
        } else {
            btn = frame.getChildByName("type_0").getChildByName("btn_1");
        }

        let btn_opacity = btn.getComponent(UIOpacity) || btn.addComponent(UIOpacity);;
        tween(btn_opacity)
            .to(0.2, { opacity: 255 })
            .to(0.2, { opacity: 0 })
            .to(0.2, { opacity: 255 })
            .to(0.2, { opacity: 0 })
            .to(0.2, { opacity: 255 })
            .to(0.2, { opacity: 0 })
            .call(() => {
                this.audioRoot.play("shake", 1);
                tween(frame)
                    .to(0.1, { angle: 5, scale: new Vec3(1.2, 1.2, 1.2) })
                    .to(0.1, { angle: 0, scale: new Vec3(1.3, 1.3, 1.3) })
                    .to(0.1, { angle: -5, scale: new Vec3(1.2, 1.2, 1.2) })
                    .to(0.1, { angle: 0, scale: new Vec3(1, 1, 1) })
                    .to(0.05, { angle: 3 })
                    .to(0.1, { angle: -3 })
                    .to(0.05, { angle: 0 })
                    .to(0.05, { angle: 3 })
                    .to(0.1, { angle: -3 })
                    .to(0.05, { angle: 0 })
                    .to(0.05, { angle: 3 })
                    .to(0.1, { angle: -3 })
                    .to(0.05, { angle: 0 })
                    .delay(1.5)
                    .call(() => {
                        this.endPlaneShakeAction();
                    })
                    .start();
            })
            .start();

        this.audioRoot.play("ding", 1);
        setTimeout(() => {
            this.audioRoot.play("ding", 1);
            setTimeout(() => {
                this.audioRoot.play("ding", 1);
            }, 200)
        }, 200)
    }

    download() {
        window.PA && window.PA.end();
        window.PA && window.PA.install();
    }
}

